//
//  L_SongClassifyViewController.m
//  音悦咖
//
//  Created by dllo on 15/7/16.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "L_SongClassifyViewController.h"
#import "MJRefresh.h"


@interface L_SongClassifyViewController ()

@property (nonatomic, retain) NSMutableArray *songClassArr1;
@property (nonatomic, retain) NSMutableArray *songClassArr2;
@property (nonatomic, retain) UICollectionReusableView *header;

@end

@implementation L_SongClassifyViewController


- (void)dealloc
{
    [_songClassArr1 release];
    [_songClassArr2 release];
    [_SongClassifyArr release];
    [_songClassifyView release];
    [_header release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
    
    
    
    
    [self.songClassifyView addHeaderWithTarget:self action:@selector(getData)];
    [self.songClassifyView addFooterWithTarget:self action:@selector(footRefresh)];
    
   [self getData];
 
}


- (void)viewWillAppear:(BOOL)animated
{
    [self.songClassifyView headerBeginRefreshing];
}



#pragma mark - 获取数据
- (void)getData
{
    NSString *str = @"http://online.dongting.com/recomm/new_albums?page=1&size=30&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.0&active=1&net=2&openudid=b55e125b1cbcd2f8d637e5573f9a6f6f241e0c3f&idfa=DD02B768-7FEA-48C3-A977-931093C926A0&utdid=VZ5%2FNAYMGNwDAGGSKGx50btO&alf=201200&bundle_id=com.ttpod.music";
    [AFNetworkHandler GETWithAFNByURL:str completion:^(id result) {
        // 歌曲分类数据
        NSArray *tempArr = [result objectForKey:@"data"];
        self.SongClassifyArr = [L_SongClassifyModel arrayWithModelByArray:tempArr];
        
        [self.songClassifyView reloadData];
    }];
    
    [self.songClassifyView headerEndRefreshing];
    
}

- (void)footRefresh
{
    [self.songClassifyView footerEndRefreshing];
}


#pragma mark - 创建视图
- (void)createView
{
    UICollectionViewFlowLayout *classifyLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.songClassifyView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:classifyLayout];
    self.songClassifyView.backgroundColor = [UIColor whiteColor];
    self.songClassifyView.dataSource = self;
    self.songClassifyView.delegate = self;
    
    [self.view addSubview:self.songClassifyView];
    [_songClassifyView release];
    
    // 注册cell重用池
    [self.songClassifyView registerClass:[L_SongClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"firstClassifyCell"];
    [self.songClassifyView registerClass:[L_SongClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"secondClassifyCell"];
    
    // 注册头部重用池
    [self.songClassifyView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"firstClassifyHeader"];
    [self.songClassifyView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"secondClassifyHeader"];
    
    
    
    
}

#pragma mark - collectionView协议代理
// 设置section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 设置item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  
    return self.SongClassifyArr.count;
}

// 设置item内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
        L_SongClassifyCollectionViewCell *firstCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstClassifyCell" forIndexPath:indexPath];

        // 赋值
        firstCell.songClassModel = self.SongClassifyArr[indexPath.item];
        
        return firstCell;

    
}

// 设置header区域内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

        if (kind == UICollectionElementKindSectionHeader) {
            UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"firstClassifyHeader" forIndexPath:indexPath];
            header.backgroundColor = [UIColor yellowColor];
            
            UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60, 0, 45, 30)];
            NSInteger num =[self.SongClassifyArr[indexPath.section] year];
            NSString *str = [NSString stringWithFormat:@"%ld",num];
            rightLabel.text = str;
            [self.header addSubview:rightLabel];
            [rightLabel release];
            
            UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 45, 30)];
            
            NSString *str1 = [NSString stringWithFormat:@"%ld/周", [self.SongClassifyArr[0] week]];
            leftLabel.text = str1;
            [header addSubview:leftLabel];
            [leftLabel release];
            
            
            return header;
        }
            
    return nil;

}

// 设置item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
 
     return CGSizeMake((SCREEN_WIDTH - 50) / 2, SCREEN_HEIGHT / 4 + 20);
}

// 设置header大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
        return CGSizeMake(SCREEN_WIDTH, 30);
}

// 设置item边距大小
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 15, 15);
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
